package view;

import domain.DomainController;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

/**
 *
 * @author Joachim
 */
public class DropboxFrame extends JFrame {
    
    private DomainController domainController;
    
    public DropboxFrame(DomainController domainController) {
        this.domainController = domainController;
        initFrame();
        initComponents();
    }
    
    private void initFrame() {
        this.setSize(400,200);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Dropbox");
    }
    
    private void initComponents() { 
        JButton btnSelectFile = new JButton("Select file");
        btnSelectFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                selectFile();
            }
        });
        add(btnSelectFile);
    }
    
    private void selectFile() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int fileChooserResult = fileChooser.showOpenDialog(this);
        if(fileChooserResult == JFileChooser.APPROVE_OPTION) {
            File fileName = fileChooser.getSelectedFile();
            String url = fileName.getPath();
            domainController.uploadFile(url);
        } 
    }
    
}
